package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.ProductWarehouseDao;
import com.xbongbong.saas.domain.entity.ProductStockUpdateEntity;
import com.xbongbong.saas.domain.entity.ProductWarehouseEntity;
import com.xbongbong.saas.enums.subform.ProductWarehouseSubFormEnum;
import com.xbongbong.saas.model.ProductWarehouseModel;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author Jian.Wang
 * @date 2019-01-25 15:38
 **/
@Service("ProductWarehouseModel")
public class ProductWarehouseModelImpl implements ProductWarehouseModel{
    private static final Logger LOG = LoggerFactory.getLogger(ProductWarehouseModelImpl.class);

    @Resource
    private ProductWarehouseDao productWarehouseDao;
    @Resource
    private EsHelper esHelper;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Deprecated
    @Override
    public Integer deleteByIdIn(String corpid, List<Long> idIn) throws XbbException {
        Integer result = productWarehouseDao.deleteByIdIn(corpid, idIn);
        paasEsModel.deleteBatch(idIn,corpid,DelEnum.DELETE.getDel(),IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
        return result;
    }

    @Override
    public Integer deleteBatch(String corpid, List<ProductWarehouseEntity> productWarehouseList) throws XbbException {
        List<Long> idInList = new ArrayList<>(productWarehouseList.size());
        productWarehouseList.forEach(item -> idInList.add(item.getId()));
        Integer result = productWarehouseDao.deleteByIdIn(corpid, idInList);
        List<InsertDTO> insertList = new ArrayList<>();
        for (ProductWarehouseEntity entity : productWarehouseList){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getProductId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("del", 1);
            map.put("updateTime", DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);
        return result;
    }

    @Override
    public Integer deleteByProductIdIn(String corpid, List<Long> productIdIn) throws XbbException {
        Integer result = productWarehouseDao.deleteByProductIdIn(corpid, productIdIn);
        List<InsertDTO> insertList = new ArrayList<>();
        List<ProductWarehouseEntity> idList = getIdList(corpid,productIdIn);
        for (ProductWarehouseEntity entity : idList){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getProductId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("del", 1);
            map.put("updateTime", DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);
        return result;
    }
    @Override
    public ProductWarehouseEntity getByKey(Integer key, String corpid) {
        return productWarehouseDao.getByKey(key, corpid);
    }

//    @Override
//    public List<ProductWarehouseEntity> findEntitys(Map<String, Object> param) {
//        return productWarehouseDao.findEntitys(param);
//    }

    @Override
    public List<ProductWarehouseEntity> findEntitysWithWarehouseChecked(Map<String, Object> param, Integer warehouseChecked) {
        param.put("warehouseChecked",warehouseChecked);
        return productWarehouseDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return productWarehouseDao.getEntitysCount(param);
    }

    @Override
    @Deprecated
    public Integer updateWarehouseNameByWarehouseId(String warehouseName, String corpid, long warehouseId) {
        return productWarehouseDao.updateWarehouseNameByWarehouseId(warehouseName, corpid, warehouseId);
    }

    @Override
    public Map<Long, ProductWarehouseEntity> getWarehouseProductList(String corpid, Long warehouseId, List<Long> productIds, List<String> fieldList) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL, DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.PRODUCT_ID), productIds));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_ID), warehouseId));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
        List<PaasFormDataEntity> list = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE,boolQueryBuilder,PaasFormDataEntity.class,fieldList);
        List<ProductWarehouseEntity> productWarehouseList = transferSubFormHelper.transferFormDataToProductWarehouseList(list);

        Map<Long, ProductWarehouseEntity> warehouseMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (ProductWarehouseEntity warehouse : productWarehouseList) {
            Long productId = warehouse.getProductId();
            if (warehouseMap.get(productId) == null) {
                warehouseMap.put(productId, warehouse);
            } else {
                ProductWarehouseEntity mergeWarehouse = warehouseMap.get(productId);
                Double mergeNum = Arith.add(mergeWarehouse.getNum(), warehouse.getNum());
                mergeWarehouse.setNum(mergeNum);
                warehouseMap.put(productId, mergeWarehouse);
            }
        }

        return warehouseMap;
    }

    @Override
    public Map<Long, List<ProductWarehouseEntity>> getWarehouseProductMapByParentIdIn(String corpid, List<Long> parentIdIn, List<String> fieldList) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL, DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.PARENT_ID), parentIdIn));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
        //fieldList.add(StringConstant.PARENTID);
        List<PaasFormDataEntity> list = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE,boolQueryBuilder,PaasFormDataEntity.class,fieldList);
        List<ProductWarehouseEntity> productWarehouseList = transferSubFormHelper.transferFormDataToProductWarehouseList(list);

        Map<Long, List<ProductWarehouseEntity>> warehouseMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (ProductWarehouseEntity warehouse : productWarehouseList) {
            Long productId = warehouse.getParentId();
            if (warehouseMap.get(productId) == null) {
                List<ProductWarehouseEntity> productWarehouse = new ArrayList<>();
                productWarehouse.add(warehouse);
                warehouseMap.put(productId, productWarehouse);
            } else {
                List<ProductWarehouseEntity> mergeWarehouse = warehouseMap.get(productId);
                mergeWarehouse.add(warehouse);
                warehouseMap.put(productId, mergeWarehouse);
            }
        }
        return warehouseMap;
    }

    @Override
    public Map<Long, List<ProductWarehouseEntity>> getWarehouseProductMap(String corpid, List<Long> productIds, List<String> fieldList) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL, DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.PRODUCT_ID), productIds));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
        List<PaasFormDataEntity> paasFormDataEntities = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE,boolQueryBuilder,PaasFormDataEntity.class,fieldList);
        List<ProductWarehouseEntity> productWarehouseList = transferSubFormHelper.transferFormDataToProductWarehouseList(paasFormDataEntities);

        Map<Long, List<ProductWarehouseEntity>> warehouseMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (ProductWarehouseEntity warehouse : productWarehouseList) {
            Long productId = warehouse.getProductId();
            if (warehouseMap.get(productId) == null) {
                List<ProductWarehouseEntity> productWarehouse = new ArrayList<>();
                productWarehouse.add(warehouse);
                warehouseMap.put(productId, productWarehouse);
            } else {
                List<ProductWarehouseEntity> mergeWarehouse = warehouseMap.get(productId);
                mergeWarehouse.add(warehouse);
                warehouseMap.put(productId, mergeWarehouse);
            }
        }
        return warehouseMap;
    }

    @Override
    public void insertBatch(List<ProductWarehouseEntity> productWarehouseList) throws XbbException {
//        for (ProductWarehouseEntity entity : productWarehouseList){
//            if (StringUtil.isEmpty(entity.getWarehouseKey())){
//                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001,"productWarehouse 唯一标识丢失");
//            }
//        }
        List<InsertDTO> insertDTOList = new ArrayList<>();
        if (productWarehouseList.size() == 1){
            productWarehouseDao.insert(productWarehouseList.get(0));
        }else {
            productWarehouseDao.insertBatch(productWarehouseList);
        }
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferProductWarehouseToFormDataList(productWarehouseList);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE));
        }

//        for (ProductWarehouseEntity productWarehouseEntity : productWarehouseList){
//            InsertDTO insertDTO = new InsertDTO();
//            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
//            insertDTO.setEsId(productWarehouseEntity.getCorpid() + "_" + productWarehouseEntity.getId());
//            insertDTO.setParent(productWarehouseEntity.getCorpid() + "_" + productWarehouseEntity.getProductId());
//            insertDTO.setSource(BeanUtil.convertBean2Map(productWarehouseEntity, false, true));
//            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
//            insertDTOList.add(insertDTO);
//        }
        if (productWarehouseList.size() == 1){
            paasEsModel.insert(insertDTOList.get(0));
        }else {
            paasEsModel.insertBatch(insertDTOList);
        }
    }

    @Override
    public void insertBatchImmediately(List<ProductWarehouseEntity> productWarehouseList) throws XbbException {
//        for (ProductWarehouseEntity entity : productWarehouseList){
//            if (StringUtil.isEmpty(entity.getWarehouseKey())){
//                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001,"productWarehouse 唯一标识丢失");
//            }
//        }
        List<InsertDTO> insertDTOList = new ArrayList<>();
        if (productWarehouseList.size() == 1){
            productWarehouseDao.insert(productWarehouseList.get(0));
        }else {
            productWarehouseDao.insertBatch(productWarehouseList);
        }
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferProductWarehouseToFormDataList(productWarehouseList);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE));
        }

//        for (ProductWarehouseEntity productWarehouseEntity : productWarehouseList){
//            InsertDTO insertDTO = new InsertDTO();
//            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
//            insertDTO.setEsId(productWarehouseEntity.getCorpid() + "_" + productWarehouseEntity.getId());
//            insertDTO.setParent(productWarehouseEntity.getCorpid() + "_" + productWarehouseEntity.getProductId());
//            insertDTO.setSource(BeanUtil.convertBean2Map(productWarehouseEntity, false, true));
//            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
//            insertDTOList.add(insertDTO);
//        }
        if (productWarehouseList.size() == 1){
            paasEsModel.insert(insertDTOList.get(0));
        }else {
            paasEsModel.insertBatch(insertDTOList, WriteRequest.RefreshPolicy.NONE);
        }
    }

    @Deprecated
    @Override
    public void updateStockBatch(List<ProductWarehouseEntity> productWarehouseEntityList, String corpid) throws XbbException {
        productWarehouseDao.updateStockBatch(productWarehouseEntityList,corpid);
        List<InsertDTO> updateDTOList = new ArrayList<>();
        for (ProductWarehouseEntity entity : productWarehouseEntityList){
            Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(ProductWarehouseSubFormEnum.NUM.getDataAttr(), entity.getNum());
            object.put(ProductWarehouseSubFormEnum.COST.getDataAttr(),entity.getCost());
            object.put(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED.getDataAttr(), entity.getWarehouseChecked());
            map.put(StringConstant.JSON_DATA, object);
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE.getType() + "_" + entity.getCorpid() + "_" + entity.getId());
            insertDTO.setParent(entity.getCorpid() + "_" + entity.getProductId());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
            updateDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(updateDTOList);
    }

    @Override
    public List<ProductWarehouseEntity> getIdList(String corpid, List<Long> productIdIn) {
        return productWarehouseDao.getIdList(corpid,productIdIn);
    }


    @Override
    public List<ProductWarehouseEntity> getProductWareHouseByParentIdAndWarehouseIdIn(String corpid, Long parentId, List<Long> warehouseIdIn, List<String> fieldList, Integer isNotify) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.PARENT_ID), parentId));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.WAREHOUSE_ID), warehouseIdIn));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL,DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
        if (Objects.nonNull(isNotify)) {
            boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.IS_NOTIFY), isNotify));
        }
        List<PaasFormDataEntity> paasFormDataEntities = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE, boolQueryBuilder, PaasFormDataEntity.class, fieldList);
        return transferSubFormHelper.transferFormDataToProductWarehouseList(paasFormDataEntities);
    }

    @Override
    public void deleteByIdInAndParentId(String corpid, List<ProductWarehouseEntity> delProductWarehouseIdList) throws XbbException {
        List<Long> idIn = new ArrayList<>();
        List<InsertDTO> updateDTOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put(StringConstant.DEL, DelEnum.DELETE.getDel());
        map.put(StringConstant.UPDATE_TIME, DateTimeUtil.getInt());
        delProductWarehouseIdList.forEach(item -> {
            idIn.add(item.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE.getType() + "_" + item.getCorpid() + "_" + item.getId());
            insertDTO.setParent(item.getCorpid() + "_" + item.getProductId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
            insertDTO.setSource(map);
            updateDTOList.add(insertDTO);
        });
        productWarehouseDao.deleteByIdIn(corpid, idIn);
        paasEsModel.updateBatch(updateDTOList);
    }

    @Override
    public List<ProductWarehouseEntity> findEntitysForUpdate(Map<String, Object> map) {
        return productWarehouseDao.findEntitysForUpdate(map);
    }

    @Override
    public void updateStockDB(Set<String> warehouseKeyList, Collection<ProductStockUpdateEntity> values, String corpid, Boolean isFromOri) throws XbbException {
        if (warehouseKeyList.isEmpty()) {
            return;
        }
        if (isFromOri) {
            productWarehouseDao.updateStockDBWithOri(warehouseKeyList,values,corpid);
        } else {
            productWarehouseDao.updateStockDB(warehouseKeyList,values,corpid);
        }
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID,corpid);
        param.put(StringConstant.DEL,DelEnum.NORMAL.getDel());
        param.put("warehouseKeyIn",warehouseKeyList);
        List<ProductWarehouseEntity> productWarehouseList = findEntitysForUpdate(param);
        if (productWarehouseList.isEmpty()) {
            return;
        }
        List<InsertDTO> updateDTOList = new ArrayList<>();
        for (ProductWarehouseEntity entity : productWarehouseList){
            Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(ProductWarehouseSubFormEnum.NUM.getDataAttr(), entity.getNum());
            object.put(ProductWarehouseSubFormEnum.COST.getDataAttr(),entity.getCost());
            object.put(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED.getDataAttr(), entity.getWarehouseChecked());
            map.put(StringConstant.JSON_DATA, object);
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE.getType() + "_" + entity.getCorpid() + "_" + entity.getId());
            insertDTO.setParent(entity.getCorpid() + "_" + entity.getProductId());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
            updateDTOList.add(insertDTO);
        }
        try {
            paasEsModel.updateBatch(updateDTOList, WriteRequest.RefreshPolicy.NONE);
        } catch (Exception e) {
            LOG.warn(e.getMessage());
        }
    }

    @Override
    public List<String> getExistWarehouseKeyList(String corpid, Set<String> warehouseKeyList, Integer warehouseChecked) {
        return productWarehouseDao.getExistWarehouseKeyList(corpid,warehouseKeyList,warehouseChecked);
    }

    /**
     * 根据warehouseKey判断是否存在
     *
     * @param corpid
     * @param warehouseKeyList
     * @param warehouseChecked
     * @return
     */
    @Override
    public List<ProductWarehouseEntity> getExistProductWarehouseList(String corpid, Set<String> warehouseKeyList, Integer warehouseChecked) {
        return productWarehouseDao.getExistProductWarehouseList(corpid,warehouseKeyList,warehouseChecked);
    }

    @Override
    public void unbundleWarehouse(String corpid, List<ProductWarehouseEntity> unbundleList) throws XbbException {
        productWarehouseDao.unbundleWarehouse(corpid,unbundleList);
        List<InsertDTO> updateDTOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        JSONObject object = new JSONObject();
        object.put(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED.getDataAttr(),0);
        map.put(StringConstant.JSON_DATA, object);
        unbundleList.forEach(item -> {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE.getType() + "_" + item.getCorpid() + "_" + item.getId());
            insertDTO.setParent(item.getCorpid() + "_" + item.getProductId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
            insertDTO.setSource(map);
            updateDTOList.add(insertDTO);
        });
        paasEsModel.updateBatch(updateDTOList);
    }

    @Override
    public void rebundleWarehouse(String corpid, List<ProductWarehouseEntity> rebundleList) throws XbbException {
        productWarehouseDao.rebundleWarehouse(corpid,rebundleList);
        List<InsertDTO> updateDTOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        rebundleList.forEach(item -> {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE.getType() + "_" + item.getCorpid() + "_" + item.getId());
            insertDTO.setParent(item.getCorpid() + "_" + item.getProductId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
            JSONObject object = new JSONObject();
            object.put(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED.getDataAttr(),1);
            object.put(ProductWarehouseSubFormEnum.IS_NOTIFY.getDataAttr(), item.getIsNotify());
            object.put(ProductWarehouseSubFormEnum.STOCK_LOWER_LIMIT.getDataAttr(), item.getStockLowerLimit());
            object.put(ProductWarehouseSubFormEnum.STOCK_UPPER_LIMIT.getDataAttr(), item.getStockUpperLimit());
            map.put(StringConstant.JSON_DATA, object);
            insertDTO.setSource(map);
            updateDTOList.add(insertDTO);
        });
        paasEsModel.updateBatch(updateDTOList);
    }

    @Override
    public List<String> getCorpidList() {
        return productWarehouseDao.getCorpidList();
    }

    @Override
    public void updateBatchNotifyMaybeCost(List<ProductWarehouseEntity> list, String corpid) throws XbbException {
        productWarehouseDao.updateBatchNotifyMaybeCost(list, corpid);
        List<InsertDTO> updateDTOList = new ArrayList<>();
        for (ProductWarehouseEntity productWarehouse : list){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE.getType() + "_" + productWarehouse.getCorpid() + "_" + productWarehouse.getId());
            insertDTO.setParent(productWarehouse.getCorpid() + "_" + productWarehouse.getProductId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE);
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(ProductWarehouseSubFormEnum.IS_NOTIFY.getDataAttr(), productWarehouse.getIsNotify());
            object.put(ProductWarehouseSubFormEnum.STOCK_LOWER_LIMIT.getDataAttr(),productWarehouse.getStockLowerLimit());
            object.put(ProductWarehouseSubFormEnum.STOCK_UPPER_LIMIT.getDataAttr(), productWarehouse.getStockUpperLimit());
            object.put(ProductWarehouseSubFormEnum.COST.getDataAttr(), productWarehouse.getCost());
            map.put(StringConstant.JSON_DATA, object);
            insertDTO.setSource(map);
            updateDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(updateDTOList);
    }

    @Override
    public void reSetOriNum(List<Long> productWarehouseIdList, String corpid) {
        productWarehouseDao.reSetOriNum(productWarehouseIdList,corpid);
    }

    @Override
    public void updateProductWarehouseById(List<ProductWarehouseEntity> productWarehouseEntityList, String corpid){
        productWarehouseDao.updateProductWarehouseById(productWarehouseEntityList, corpid);
    }

    @Override
    public List<ProductWarehouseEntity> findProductWarehouse(List<Long> prodcutIdList, String corpid){
        return productWarehouseDao.findProductWarehouse(prodcutIdList, corpid);
    }

    @Override
    public List<ProductWarehouseEntity> listByProductIdInAndWarehouseIdIn(String corpid, List<Long> productIdIn, List<Long> warehouseIdIn) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.PRODUCT_ID), productIdIn));
        boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.WAREHOUSE_ID), warehouseIdIn));
        boolQueryBuilder.filter(termQuery(StringConstant.DEL,DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getAttrConnectData(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
        List<PaasFormDataEntity> paasFormDataEntities = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE, boolQueryBuilder, PaasFormDataEntity.class, new ArrayList<>());
        return transferSubFormHelper.transferFormDataToProductWarehouseList(paasFormDataEntities);
    }

    @Override
    public List<ProductWarehouseEntity> listByProductIdInWasehouseIn(String corpid, List<String> warehouseKey){
        return productWarehouseDao.listByProductIdInWasehouseIn(corpid, warehouseKey);
    }

    @Override
    public List<ProductWarehouseEntity> getCostIsNullWarehouseKey(String corpid, List<Long> productIdList){
        return productWarehouseDao.getCostIsNullWarehouseKey(corpid, productIdList);
    }
}
